package com.ellricky.baselib.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.andr.common.util.EmptyUtil;
import com.andr.common.util.ScreenUtil;
import com.andr.common.util.ViewUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.listener.OnTakePhotoListener;
import com.ellricky.baselib.util.EDialogUtil;
import com.ellricky.baselib.util.GlideUtil;
import com.ellricky.baselib.util.BigIvUtil;
import com.flyco.roundview.RoundFrameLayout;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;

public class AvatarView extends FrameLayout implements View.OnClickListener {

    private Context mContext;
    private RoundFrameLayout addView;
    private RoundedImageView ivPic;

    private OnTakePhotoListener listener;

    private String url;
    private boolean enablePreview;

    public void setListener(OnTakePhotoListener listener) {
        this.listener = listener;
    }

    public AvatarView(Context context) {
        super(context);
        initView(context);
    }

    public AvatarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        mContext = context;
        View.inflate(mContext, R.layout.base_avatar_view, this);

        addView = findViewById(R.id.add_view);
        ivPic = findViewById(R.id.iv_pic);

        addView.setOnClickListener(this);
        ivPic.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (ViewUtil.isFastDoubleClick()) return;
        if (view == addView) {
            EDialogUtil.showTakePhotoDialog(mContext, listener);
            return;
        }

        if (view == ivPic) {
            if (!enablePreview) return;
            ArrayList<String> list = new ArrayList<>();
            list.add(url);
            BigIvUtil.jumpBigIvPage((Activity) mContext, list, 0);
        }
    }

    public void enableClick(boolean enable, ArrayList<String> urlList, int position) {
        if (enable) {
            BigIvUtil.jumpBigIvPage((Activity) mContext, urlList, position);
        }
    }


    public void setPic(String url) {
        setPic(url, true);
    }

    public void setPic(String url, boolean enablePreview) {
        this.url = url;
        if (EmptyUtil.isEmpty(url)) {
            addView.setVisibility(VISIBLE);
            ivPic.setVisibility(GONE);
        } else {
            this.enablePreview = enablePreview;
            addView.setVisibility(GONE);
            ivPic.setVisibility(VISIBLE);
            GlideUtil.into((Activity) mContext, url, ivPic);
        }

    }


    public void initBg(float dp) {
        addView.getDelegate().setCornerRadius(
                (int) ScreenUtil.dpUnit(mContext, dp)
        );

        ivPic.setCornerRadius((int) ScreenUtil.dpUnit(mContext, dp));
    }

    public String getUrl() {
        return url;
    }
    //    public RoundedImageView getIvPic() {
//        addView.setVisibility(GONE);
//        ivPic.setVisibility(VISIBLE);
//        return ivPic;
//    }
//
//    public RoundFrameLayout getAddView() {
//        addView.setVisibility(VISIBLE);
//        ivPic.setVisibility(GONE);
//        return addView;
//    }
}
